package com.example.loadermanager

import android.os.Bundle
import android.widget.ArrayAdapter
import androidx.appcompat.app.AppCompatActivity
import androidx.loader.app.LoaderManager
import com.example.loadermanager.loader.ContactsLoader
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    private lateinit var loaderManager: LoaderManager
    private val ID = 100
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val adapter = ArrayAdapter<String>(this, android.R.layout.simple_list_item_1).apply {
            listView.adapter = this
        }
        val contactsLoader = ContactsLoader(this) {
            adapter.addAll(it)
            adapter.notifyDataSetChanged()
        }

        loaderManager = LoaderManager.getInstance(this).apply {
            initLoader(ID, null, contactsLoader)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        loaderManager.destroyLoader(ID)
    }
}